package com.skeyedu.book.controller;


import com.skeyedu.book.entity.Book;
import com.skeyedu.book.service.BookService;
import com.skeyedu.book.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/book")
public class BookController {

    private static BookService bookService;

    //获取所有出售中的书本列表
    @GetMapping("/getBooksOnSelling")
    public Object getBooksOnSelling(){

        List<Book> books= bookService.getBooksOnSelling();
        if(books!=null) {
            return Result.ok("books",books);
        }else {
            return Result.fail();
        }

    }

    //按书名获取所有出售中的书本列表
    @GetMapping("/getBooksByTitle")
    public Object getBooksByTitle(@RequestParam String title){
        List<Book> books = bookService.getBooksByTitle(title);
        if(books != null){
            return Result.ok("books",books);
        }else {
            return Result.fail();
        }

    }
    //上架图书
    @PostMapping("/addBook")
    public Object addBook(@RequestBody Book book){
        int result = bookService.addBook(book);
        if(result >=1){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    @Autowired
    public void setBookService(BookService bookService){
        BookController.bookService=bookService;
    }
}
